package com.ss.android.article.base.feature.video;

import android.support.annotation.NonNull;
import android.util.Pair;
import android.util.SparseArray;

import com.ss.android.l.a;
import com.ss.ttvideoengine.b.VideoInfo;
import com.ss.ttvideoengine.b.VideoItem;

public class VideoClarityHelper {
    public static final int VIDEO_CLARITY_FAST = 2;
    public static final int VIDEO_CLARITY_FLUENCY = 1;
    public static final int VIDEO_CLARITY_HD = 0;
    public static final int VIDEO_CLARITY_UNKNOW = -1;
    private static boolean isAutoMode;
    private static VideoItem sCurrentVideoInfo;
    @VideoClarity
    private static int sDefaultOfflineClarity;
    @VideoClarity
    private static int sVideoClarity;

    static {
        VideoClarityHelper.sVideoClarity = -1;
        VideoClarityHelper.sDefaultOfflineClarity = -1;
        VideoClarityHelper.isAutoMode = true;
    }

    public VideoClarityHelper() {

    }

    public static boolean canPlayHDVideo() {
        boolean v0 = false;
        if (a.a() >= 4 && a.b() >= 1000000) {
            v0 = true;
        }

        return v0;
    }

    @NonNull
    public static Pair convertDefinition(String arg2) {
        Pair v0;
        if ("360p".equals(arg2)) {
            v0 = Pair.create("极速", Integer.valueOf(2));
        } else if ("480p".equals(arg2)) {
            v0 = Pair.create("流畅", Integer.valueOf(1));
        } else if ("720p".equals(arg2)) {
            v0 = Pair.create("高清", Integer.valueOf(0));
        } else {
            v0 = Pair.create("", Integer.valueOf(-1));
        }

        return v0;
    }

    private static VideoItem getAutoModeVideoInfo(SparseArray<VideoItem> sparseArray) {
        if (sparseArray == null || sparseArray.get(2) == null) {
            return null;
        }
        return (VideoItem) sparseArray.get(2);
    }

    @VideoClarity
    public static int getClarity(String str) {
        return ((Integer) convertDefinition(str).second).intValue();
    }

    public static VideoItem getCurrentVideoInfo() {
        return VideoClarityHelper.sCurrentVideoInfo;
    }

    @VideoClarity
    public static int getDefaultVideoClarity() {
        int v0 = 2;
        if ((VideoClarityHelper.canPlayHDVideo()) && VideoClarityHelper.sVideoClarity != -1) {
            v0 = VideoClarityHelper.sVideoClarity;
        }

        return v0;
    }

    public static void setDefaultVideoClarity(@VideoClarity int arg1) {
        if (arg1 != -1) {
            VideoClarityHelper.sVideoClarity = arg1;
            VideoClarityHelper.isAutoMode = false;
        }
    }

    public static String getDefinitionStr(String arg1) {
        return (String) VideoClarityHelper.convertDefinition(arg1).first;
    }

    public static SparseArray<VideoItem> getSupportVideoInfos(VideoInfo videoInfo) {
        SparseArray<VideoItem> videoItemSparseArray = new SparseArray();
        if (videoInfo != null) {
            if (videoInfo.mVideoItem1 != null) {
                videoItemSparseArray.put(2, videoInfo.mVideoItem1);
            }

            if (videoInfo.mVideoItem2 != null) {
                videoItemSparseArray.put(1, videoInfo.mVideoItem2);
            }

            if (videoInfo.mVideoItem3 == null) {
                return videoItemSparseArray;
            }
            videoItemSparseArray.put(0, videoInfo.mVideoItem3);
        }

        return videoItemSparseArray;
    }

    public static VideoItem getVideoInfo(VideoInfo arg1, @VideoClarity int arg2) {
        return VideoClarityHelper.getSupportVideoInfos(arg1).get(arg2);
    }

    public static VideoItem getVideoInfoWithClarity(SparseArray<VideoItem> videoItemSparseArray) {
        if (isAutoMode) {
            sCurrentVideoInfo = VideoClarityHelper.getAutoModeVideoInfo(videoItemSparseArray);
            return sCurrentVideoInfo;
        } else {
            int videoClarity = VideoClarityHelper.sVideoClarity;
            while (true) {
                if (videoClarity <= 2) {
                    VideoItem videoItem = videoItemSparseArray.get(videoClarity);
                    if (videoItem != null) {
                        sCurrentVideoInfo = videoItem;
                        return sCurrentVideoInfo;
                    } else {
                        ++videoClarity;
                        continue;
                    }
                } else {
                    VideoItem videoItem = videoItemSparseArray.get(2);
                    sCurrentVideoInfo = videoItem;
                    return sCurrentVideoInfo;
                }
            }
        }
    }

    public static boolean isAutoMode() {
        return VideoClarityHelper.isAutoMode;
    }

    public @interface VideoClarity {
    }
}

